using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Diagnostics;
using System.Windows.Forms;

using Janus.Windows.TimeLine;
using Janus.Windows.EditControls;
namespace Janus.NorthwindDemo2007
{
	public partial class TimescaleForm
	{

		protected override void OnLoad(System.EventArgs e)
		{
			base.OnLoad(e);
			NorthwindApp.MainForm.VisualStyleManager1.AddControl(this, true);
		}
		private Janus.Windows.TimeLine.TimeLine mTimeLine = null;

		private void PopulateCombos()
		{

			cboTimescaleTiers.Items.Add(Janus.Windows.TimeLine.TimescaleTiers.ThreeTiers);
			cboTimescaleTiers.Items.Add(Janus.Windows.TimeLine.TimescaleTiers.TwoTiers);
			cboTimescaleTiers.Items.Add(Janus.Windows.TimeLine.TimescaleTiers.OneTier);

			this.cboTimescaleTiers.SelectedValue = this.mTimeLine.TimescaleTiers;

			cboTopTierInterval.Items.Add(Janus.Windows.TimeLine.TimeLineInterval.Second);
			cboTopTierInterval.Items.Add(Janus.Windows.TimeLine.TimeLineInterval.Minute);
			cboTopTierInterval.Items.Add(Janus.Windows.TimeLine.TimeLineInterval.Hour);
			cboTopTierInterval.Items.Add(Janus.Windows.TimeLine.TimeLineInterval.Day);
			cboTopTierInterval.Items.Add(Janus.Windows.TimeLine.TimeLineInterval.Week);
			cboTopTierInterval.Items.Add(Janus.Windows.TimeLine.TimeLineInterval.Month);
			cboTopTierInterval.Items.Add(Janus.Windows.TimeLine.TimeLineInterval.Year);

			this.cboTopTierInterval.SelectedValue = this.mTimeLine.TopTier.Interval;

			cboMiddleTierInterval.Items.Add(Janus.Windows.TimeLine.TimeLineInterval.Second);
			cboMiddleTierInterval.Items.Add(Janus.Windows.TimeLine.TimeLineInterval.Minute);
			cboMiddleTierInterval.Items.Add(Janus.Windows.TimeLine.TimeLineInterval.Hour);
			cboMiddleTierInterval.Items.Add(Janus.Windows.TimeLine.TimeLineInterval.Day);
			cboMiddleTierInterval.Items.Add(Janus.Windows.TimeLine.TimeLineInterval.Week);
			cboMiddleTierInterval.Items.Add(Janus.Windows.TimeLine.TimeLineInterval.Month);
			cboMiddleTierInterval.Items.Add(Janus.Windows.TimeLine.TimeLineInterval.Year);

			this.cboMiddleTierInterval.SelectedValue = this.mTimeLine.MiddleTier.Interval;

			cboBottomTierInterval.Items.Add(Janus.Windows.TimeLine.TimeLineInterval.Second);
			cboBottomTierInterval.Items.Add(Janus.Windows.TimeLine.TimeLineInterval.Minute);
			cboBottomTierInterval.Items.Add(Janus.Windows.TimeLine.TimeLineInterval.Hour);
			cboBottomTierInterval.Items.Add(Janus.Windows.TimeLine.TimeLineInterval.Day);
			cboBottomTierInterval.Items.Add(Janus.Windows.TimeLine.TimeLineInterval.Week);
			cboBottomTierInterval.Items.Add(Janus.Windows.TimeLine.TimeLineInterval.Month);
			cboBottomTierInterval.Items.Add(Janus.Windows.TimeLine.TimeLineInterval.Year);

			this.cboBottomTierInterval.SelectedValue = this.mTimeLine.BottomTier.Interval;
		}

		private void PopulateCustomFormatCombo(UIComboBox comboBox, TimeLineInterval interval)
		{

			comboBox.Items.Clear();
			comboBox.DisplayMember = "Text";
			comboBox.ValueMember = "Value";
			comboBox.Items.Add(new UIComboBoxItem("Default", ""));
			switch (interval)
			{
				case TimeLineInterval.Day:
					comboBox.Items.Add(new UIComboBoxItem("S, M...", "W"));
					comboBox.Items.Add(new UIComboBoxItem("S 18, M 19...", "W dd"));
					comboBox.Items.Add(new UIComboBoxItem("18, 19...", "dd"));
					comboBox.Items.Add(new UIComboBoxItem("Sun, Mon...", "ddd"));
					comboBox.Items.Add(new UIComboBoxItem("Sunday, Monday...", "dddd"));
					comboBox.Items.Add(new UIComboBoxItem("Jan 18, Jan 19...", "MMM dd"));
					comboBox.Items.Add(new UIComboBoxItem("Sunday, January 18, 2004...", "D"));
					break;
				case TimeLineInterval.Hour:
					comboBox.Items.Add(new UIComboBoxItem("01, 02...", "HH"));
					comboBox.Items.Add(new UIComboBoxItem("Hour 1, Hour 2...", "'Hour' H"));
					comboBox.Items.Add(new UIComboBoxItem("1 AM, 2 AM...", "h tt"));
					comboBox.Items.Add(new UIComboBoxItem("1:00 AM, 2:00 AM...", "hh:mm tt"));
					comboBox.Items.Add(new UIComboBoxItem("1/18 1 AM, 1/19 2 AM", "M/dd h tt"));
					break;
				case TimeLineInterval.Minute:
					comboBox.Items.Add(new UIComboBoxItem("01, 02...", "mm"));
					comboBox.Items.Add(new UIComboBoxItem("Minute 01, Minute 02...", "'Minute' mm"));
					comboBox.Items.Add(new UIComboBoxItem("01:01 AM, 01:02 AM...", "hh:mm tt"));
					comboBox.Items.Add(new UIComboBoxItem("1/18 01:01 AM, 1/18 01:02 AM...", "M/dd hh:mm tt"));
					break;
				case TimeLineInterval.Month:
					comboBox.Items.Add(new UIComboBoxItem("January 2004, February 2004...", "MMMM yyy"));
					comboBox.Items.Add(new UIComboBoxItem("January 04, February 04...", "MMMM yy"));
					comboBox.Items.Add(new UIComboBoxItem("January, February...", "MMMM"));
					comboBox.Items.Add(new UIComboBoxItem("Jan, Feb...", "MMM"));
					break;
				case TimeLineInterval.Second:
					comboBox.Items.Add(new UIComboBoxItem("01, 02...", "ss"));
					comboBox.Items.Add(new UIComboBoxItem("Second 01, Second 02...", "'Second' ss"));
					comboBox.Items.Add(new UIComboBoxItem("01:01:01 AM, 01:01:02 AM...", "hh:mm:ss tt"));
					comboBox.Items.Add(new UIComboBoxItem("1/18 01:01:01 AM, 1/18 01:01:02 AM...", "M/dd hh:mm:ss tt"));
					break;
				case TimeLineInterval.Week:
					comboBox.Items.Add(new UIComboBoxItem("Jan 18 04, Jan 25 04...", "MMM dd, yy"));
					comboBox.Items.Add(new UIComboBoxItem("January 18 04, January 25 04...", "MMMM dd, yy"));
					comboBox.Items.Add(new UIComboBoxItem("January 18, January 25...", "MMMM dd"));
					comboBox.Items.Add(new UIComboBoxItem("Sunday 18, Sunday 25...", "dddd dd"));
					comboBox.Items.Add(new UIComboBoxItem("Sun 18, Sun 25", "ddd dd"));
					comboBox.Items.Add(new UIComboBoxItem("01/18, 01/25...", "MM/dd"));
					break;
				case TimeLineInterval.Year:
					comboBox.Items.Add(new UIComboBoxItem("2004, 2005...", "yyy"));
					comboBox.Items.Add(new UIComboBoxItem("04, 05...", "yy"));
					comboBox.Items.Add(new UIComboBoxItem("Year 2004, Year 2005...", "'Year' yyy"));
					comboBox.Items.Add(new UIComboBoxItem("Year 04, Year 05...", "'Year' yy"));
					break;
			}
		}

		public void ShowTimescaleFormDialog(Janus.Windows.TimeLine.TimeLine timeLine)
		{
			this.mTimeLine = timeLine;

			this.PopulateCombos();

			this.nudSize.Value = timeLine.IntervalSize;

			this.nudTopTierCount.Value = this.mTimeLine.TopTier.Count;
			this.nudMiddleTierCount.Value = this.mTimeLine.MiddleTier.Count;
			this.nudBottomTierCount.Value = this.mTimeLine.BottomTier.Count;

			this.cboBottomTierFormat.SelectedValue = this.mTimeLine.BottomTier.CustomFormat;
			this.cboMiddleTierFormat.SelectedValue = this.mTimeLine.MiddleTier.CustomFormat;
			this.cboTopTierFormat.SelectedValue = this.mTimeLine.TopTier.CustomFormat;

			this.ShowDialog();
		}

		private void TimeScaleForm_Closing(object sender, System.ComponentModel.CancelEventArgs e)
		{
			if (this.DialogResult == System.Windows.Forms.DialogResult.OK)
			{

				this.mTimeLine.SuspendTimescaleValidation();
				this.mTimeLine.TimescaleTiers = (TimescaleTiers)this.cboTimescaleTiers.SelectedValue;
				this.mTimeLine.IntervalSize = this.nudSize.Value;

				this.mTimeLine.TopTier.Interval = (TimeLineInterval)this.cboTopTierInterval.SelectedValue;
				this.mTimeLine.TopTier.Count = this.nudTopTierCount.Value;
				this.mTimeLine.TopTier.CustomFormat = (string)this.cboTopTierFormat.SelectedValue;

				this.mTimeLine.MiddleTier.Interval = (TimeLineInterval)this.cboMiddleTierInterval.SelectedValue;
				this.mTimeLine.MiddleTier.Count = this.nudMiddleTierCount.Value;
				this.mTimeLine.MiddleTier.CustomFormat = (string)this.cboMiddleTierFormat.SelectedValue;

				this.mTimeLine.BottomTier.Interval = (TimeLineInterval)this.cboBottomTierInterval.SelectedValue;
				this.mTimeLine.BottomTier.Count = this.nudBottomTierCount.Value;
				this.mTimeLine.BottomTier.CustomFormat = (string)this.cboBottomTierFormat.SelectedValue;

				try
				{
					this.mTimeLine.ResumeTimescaleValidation();
				}
				catch
				{
					MessageBox.Show(this, "An upper tier has less time per tick than one of the tiers below it." + "\r\n" + "Try the following:" + "\\n" + "-Change the value in the tier's interval box." + "\r\n" + "\\n" + "-Change the number of units you want displayed in the tier's count box.", NorthwindApp.MessageCaption, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
					e.Cancel = true;
				}
			}
		}

		private void cboTimescaleTiers_SelectedIndexChanged(object sender, System.EventArgs e)
		{

			switch ((TimescaleTiers)this.cboTimescaleTiers.SelectedValue)
			{
				case TimescaleTiers.ThreeTiers:
					this.grbTopTier.Enabled = true;
					this.grbMiddleTier.Enabled = true;
					break;
				case TimescaleTiers.OneTier:
					this.grbTopTier.Enabled = false;
					this.grbMiddleTier.Enabled = false;
					break;
				case TimescaleTiers.TwoTiers:
					this.grbTopTier.Enabled = false;
					this.grbMiddleTier.Enabled = true;
					break;
			}
		}

		private void cboTopTierInterval_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			this.PopulateCustomFormatCombo(this.cboTopTierFormat, (TimeLineInterval)cboTopTierInterval.SelectedValue);
			this.cboTopTierFormat.SelectedValue = this.mTimeLine.TopTier.CustomFormat;
		}

		private void cboMiddleTierInterval_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			this.PopulateCustomFormatCombo(this.cboMiddleTierFormat, (TimeLineInterval)cboMiddleTierInterval.SelectedValue);

			this.cboMiddleTierFormat.SelectedValue = this.mTimeLine.MiddleTier.CustomFormat;

			if (cboTopTierInterval.SelectedIndex < this.cboMiddleTierInterval.SelectedIndex)
			{
				if (cboMiddleTierInterval.SelectedIndex == this.cboMiddleTierInterval.Items.Count - 1)
				{
					cboTopTierInterval.SelectedIndex = this.cboMiddleTierInterval.SelectedIndex;
				}
				else
				{
					cboTopTierInterval.SelectedIndex = this.cboMiddleTierInterval.SelectedIndex + 1;
				}
			}
		}

		private void cboBottomTierInterval_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			this.PopulateCustomFormatCombo(this.cboBottomTierFormat, (TimeLineInterval)cboBottomTierInterval.SelectedValue);
			this.cboBottomTierFormat.SelectedValue = this.mTimeLine.BottomTier.CustomFormat;
			if (cboMiddleTierInterval.SelectedIndex < this.cboBottomTierInterval.SelectedIndex)
			{
				if (cboBottomTierInterval.SelectedIndex == this.cboBottomTierInterval.Items.Count - 1)
				{
					cboMiddleTierInterval.SelectedIndex = this.cboBottomTierInterval.SelectedIndex;
				}
				else
				{
					cboMiddleTierInterval.SelectedIndex = this.cboBottomTierInterval.SelectedIndex + 1;
				}
			}
		}

	}

} //end of root namespace